/*
 * @Author: your name
 * @Date: 2021-01-29 15:08:34
 * @LastEditTime: 2021-02-02 17:49:30
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \wechat-node\app.js
 */
const Koa = require('koa');
const app = new Koa();
const json = require('koa-json');
const onerror = require('koa-onerror');

const KoaStatic = require('koa-static');
// 请求参数插件
const koaBody = require('koa-body');
// const logger = require('koa-logger');
// 返回参数封装
const resParser = require('./middleWare/res');

// 路由
const index = require('./routes/index');

// session处理
const session = require('koa-session');
const sessionConfig = require('./config/sessionConfig');

// 跨域处理
const cors = require('koa2-cors');
const corsConfig = require('./config/cors');
// 分页处理
const pageRes = require('./middleWare/pagination');
// error handler
onerror(app);

app.keys = ['secret'];
app.use(session(sessionConfig, app));
app.use(cors(corsConfig))

// middlewares
app.use(koaBody({
	multipart: true,
}));
app.use(json());
// app.use(logger());
app.use(resParser);

app.use(pageRes);

app.use(index.routes(), index.allowedMethods());

app.use(KoaStatic('./public/'));
console.log('ssssss', __dirname);

// 处理404
app.use(async (ctx) => {
	ctx.notFound('请求url错误');
});

module.exports = app;
